package superhero.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import superhero.data.SuperheroDBQuery;

public class SuperheroDisplayPanel
        extends JFrame
{
    private JButton searchButton;
    private JComboBox groups, characters;
    private JLabel picHolder1, picHolder2, picHolder3;
    private JTabbedPane mainPanel;
    private JPanel startPanel, comboPanel, organizer;
    private JPanel borders = new JPanel(new BorderLayout());
    private ComboBoxModel[] model = new ComboBoxModel[3];
    private Dimension size =  new Dimension(1200, 600);
    private Dimension empty = new Dimension(100, 85);
    private MenuBar menu = new MenuBar();
    /**
     * This constructor builds the GUI displayed to the user. 
     */
    public SuperheroDisplayPanel()
    {
        setTitle("Superhero Registry");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainPanel = new JTabbedPane();
        this.add(mainPanel);
        mainPanel.setPreferredSize(size);
        buildComboBox();
        buildStartPanel();
        organizer = new JPanel(new BorderLayout());
        organizer.add(startPanel, BorderLayout.NORTH);
        mainPanel.addTab("Start Page", organizer);
        
        setJMenuBar(menu.getMenuBar());
        this.setResizable(false);
        this.pack();
        this.setVisible(true);
    }
    
    /**
     * This method builds and populates the ComboBoxes.
     * It places the boxes in a GUI alongside a JButton
     * to search the DATABASE for specific heroes.
     */    
    private void buildStartPanel()
    {
        startPanel = new JPanel(new BorderLayout());
        
        String picName = "Images//XMen.jpg";
        ImageIcon picture = new ImageIcon(picName);
        picHolder1 = new JLabel(picture);
                
        String pixName = "Images//Avengers.jpg";
        ImageIcon pix = new ImageIcon(pixName);
        picHolder2 = new JLabel(pix);
        
        String pictName = "Images//justiceleague.jpg";
        ImageIcon pict = new ImageIcon(pictName);
        picHolder3 = new JLabel(pict);
        
        startPanel.add(borders, BorderLayout.CENTER);        
        startPanel.add(picHolder1, BorderLayout.EAST);
        startPanel.add(picHolder2, BorderLayout.WEST);
        startPanel.add(picHolder3, BorderLayout.NORTH);        
    }
    
    private void buildComboBox()
    {
        Dimension space = new Dimension(25, 85);
        
        comboPanel = new JPanel();

        JPanel temp1 = new JPanel();        
        temp1.setPreferredSize(space);
        
        borders.add(temp1, BorderLayout.NORTH);
        borders.add(comboPanel, BorderLayout.CENTER);
        
                String fact = "SELECT FactionName "
                + "FROM Factions ";

        SuperheroDBQuery faction =
                new SuperheroDBQuery(fact);
        
        model[2] = new DefaultComboBoxModel(faction.getRecords());
        
        groups = new JComboBox();
        groups.setModel(model[2]);
        
        String start = "SELECT CharacterName "
                + "FROM Characters ORDER BY CharacterName";
        SuperheroDBQuery begin =
                new SuperheroDBQuery(start);

        model[0] = new DefaultComboBoxModel(begin.getRecords());

        comboPanel.add(groups);
        characters = new JComboBox(begin.getRecords());
        characters.setModel(model[0]);
        comboPanel.add(characters);
        groups.addActionListener(new changedItemActionListener());
        searchButton = new JButton("Search");
        searchButton.addActionListener(new SearchButtonListener());
        comboPanel.add(searchButton);    
    }
    
    private class SearchButtonListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            boolean isCreated = false;
            String character = characters.getSelectedItem().toString();
            CreateNewTab tab = new CreateNewTab(characters.getSelectedItem());
            ButtonTabComponent pane = new ButtonTabComponent(mainPanel);
            JPanel buffer = new JPanel(new BorderLayout());
            JPanel spacer = new JPanel();
            buffer.add(spacer, BorderLayout.NORTH);
            spacer.setPreferredSize(empty);
            buffer.add(tab.getNewTab(), BorderLayout.CENTER);
            
            for(int i = 0; i < mainPanel.getTabCount(); i++)
            {
                if(mainPanel.getTitleAt(i).equalsIgnoreCase(character))
                {
                    isCreated = true;
                    mainPanel.setSelectedIndex(i);
                    break;
                }
            }
            
            if(isCreated == false)
            {
                mainPanel.addTab(character, buffer);
                int lastIndex = mainPanel.getTabCount() - 1; 
                mainPanel.setTabComponentAt(lastIndex, pane);       
                pack();
            }
                
        }
    }

    private class changedItemActionListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {
            if ((groups.getSelectedItem().
                    equals(groups.getItemAt(0))))
            {
                String start = "SELECT CharacterName "
                        + "FROM Characters ORDER BY CharacterName";
                SuperheroDBQuery begin =
                        new SuperheroDBQuery(start);

                model[0] = new DefaultComboBoxModel(begin.getRecords());
                groups.setModel(model[2]);
                characters.setModel(model[0]);
            }
            else
            {
                String query = "SELECT CharacterName "
                        + "FROM Characters, Factions "
                        + "WHERE factions.factionName = '"
                        + groups.getSelectedItem().
                        toString() + "' AND "
                        + "characters.FactionID = factions.FactionID "
                        + "ORDER BY characters.CharacterName";

                SuperheroDBQuery answer =
                        new SuperheroDBQuery(query);

                model[1] = new DefaultComboBoxModel(answer.getRecords());
                groups.setModel(model[2]);
                characters.setModel(model[1]);
            }

        }
    }
}